package com.tiantian.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tiantian.common.core.domain.ResponseResult;
import com.tiantian.common.core.domain.page.TableDataInfo;
import com.tiantian.common.excel.utils.ExcelUtil;
import com.tiantian.common.log.annotation.Log;
import com.tiantian.common.log.enums.BusinessType;
import com.tiantian.common.satoken.domain.controller.BaseController;
import com.tiantian.system.domain.entity.SysLogininfor;
import com.tiantian.system.domain.entity.SysOperLog;
import com.tiantian.system.service.SysAccessLogService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统访问日志
 * @author tiantian
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/log/accessLog")
@SaCheckPermission("log")
public class SysAccessLogController extends BaseController {

    private final SysAccessLogService sysAccessLogService;

    /**
     * 获取操作日志记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysLogininfor logininfor) {
        startPage();
        List<SysLogininfor> sysOperLogs = sysAccessLogService.selectAccessLogList(logininfor);
        return getDataTable(sysOperLogs);
    }

    @GetMapping("/export")
    @Log(businessType = BusinessType.EXP_LOG, isSaveResponseData = false, isSaveRequestData = false)
    public void exportLog(HttpServletResponse response){
        List<SysLogininfor> selectAccessLogList = sysAccessLogService.selectAccessLogList(null);
        ExcelUtil.exportExcel(selectAccessLogList, "登录日志", SysLogininfor.class, response);
    }

    /**
     * 批量删除操作日志记录
     */
    @DeleteMapping("/del")
    @Log(businessType = BusinessType.DEL_LOG)
    public ResponseResult<Void> remove(List<Long> operIds) {
        return toAjax(sysAccessLogService.deleteOperLogByIds(operIds));
    }

    /**
     * 清理操作日志记录
     */
    @DeleteMapping("/clean")
    @Log(businessType = BusinessType.CLEAR_LOG)
    public ResponseResult<Void> clean() {
        sysAccessLogService.cleanLog();
        return ResponseResult.ok();
    }
}
